package client.gui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.text.*;
import java.sql.Connection;
import client.*;
import client.dao.*;
import project.*;

public class LookClientPanel extends JPanel implements ActionListener,
														FocusListener
{
	private JPanel formP = null, buttonP = null, westP = null,
					eastP = null, searchP = null;
	private JButton editB = null, delB = null, saveB = null;
	private JLabel searchL = null;
	private JTextField searchT = null;
	private JComboBox jcb = null;
	private JButton searchB = null;
	private JDialog custDialog = null;
	private JTable custTable = null;
	private JButton dialogAddB = null, dialogCancelB = null;
	DetailsPanel dp = null;
	AddressPanel ap1 = null;
	Client client = null;
	ClientManager cm = null;
	Connection conn = null;
	JFrame frame;
	SideInfoPanel cp = null;
	
	public LookClientPanel(JFrame frame, Connection conn)
	{
		this.frame = frame;
		this.conn = conn;
		this.setLayout(new BorderLayout());
		
		dp = new DetailsPanel("Look");
		ap1 = new AddressPanel("Residential");
		buttonP = new JPanel();
		editB = new JButton("Edit details");
		editB.addActionListener(this);
		delB = new JButton("Delete");
		delB.addActionListener(this);
		saveB = new JButton("Save");
		saveB.addActionListener(this);
		buttonP.add(editB);
		buttonP.add(delB);
		buttonP.setBorder(BorderFactory.createEtchedBorder());
		formP = new JPanel(new BorderLayout());
		formP.add(dp, BorderLayout.NORTH);
		formP.add(ap1, BorderLayout.CENTER);
		westP = new JPanel(new BorderLayout());
		westP.add(formP, BorderLayout.CENTER);
		westP.add(buttonP, BorderLayout.SOUTH);
		westP.setPreferredSize(new Dimension(280,450));
		westP.setBorder(BorderFactory.createEmptyBorder(0,1,0,0));
		
		eastP = new JPanel(new GridLayout(1,1));
		eastP.setPreferredSize(new Dimension(500, 500));
		searchP = new JPanel();
		searchL = new JLabel("Search: ");
		searchT = new JTextField(40);
		searchT.addActionListener(this);
		String [] jcbString = new String[]{
				"Client ID", "Last Name", "Phone No"};
		jcb = new JComboBox(jcbString);
		searchB = new JButton("Search");
		searchB.addActionListener(this);
		searchP.add(searchL);
		searchP.add(searchT);
		searchP.add(jcb);
		searchP.add(searchB);
		searchP.setBorder(BorderFactory.createEtchedBorder());
		//searchP.setBorder(BorderFactory.createCompoundBorder(
		//	BorderFactory.createEmptyBorder(10,10,10,10),
		//	BorderFactory.createRaisedBevelBorder()));
		
		//PhotoPanel pp = new PhotoPanel();
		
		//eastP.add(searchP, BorderLayout.NORTH);
		//eastP.add(pp, BorderLayout.SOUTH);
		
		this.add(searchP, BorderLayout.NORTH);
		this.add(westP, BorderLayout.CENTER);
		this.add(eastP, BorderLayout.EAST);
		
		dp.clientIDT.addFocusListener(this);
		dp.surnameT.addFocusListener(this);
		dp.nameT.addFocusListener(this);
		dp.initialsT.addFocusListener(this);
		dp.idNumberT.addFocusListener(this);
		dp.phoneOneT.addFocusListener(this);
		dp.phoneTwoT.addFocusListener(this);
		dp.emailT.addFocusListener(this);
		dp.titleJCB.addFocusListener(this);
		dp.custStatusJCB.addFocusListener(this);
		dp.jdc.addFocusListener(this);
		ap1.streetT.addFocusListener(this);
		ap1.suburbT.addFocusListener(this);
		ap1.cityT.addFocusListener(this);
		ap1.posCodeT.addFocusListener(this);
		
		setFieldsSelectable(false);
		dialogAddB = new JButton("SELECT");
		dialogAddB.addActionListener(this);
		dialogCancelB = new JButton("CANCEL");
		dialogCancelB.addActionListener(this);
		cm = new ClientManager(conn);
		dp.clientIDT.setBackground(new Color(173, 190, 230));
		dp.newPasswordB.addActionListener(this);
		searchT.requestFocus();
		
	}
	public DetailsPanel getDetailsPanel()
	{
		return dp;
	}
	public AddressPanel getAddressPanel()
	{
		return ap1;
	}
	public void setFieldsSelectable(Boolean tf)
	{
		if(tf)
		{
			dp.surnameT.setFocusable(true);
			dp.nameT.setFocusable(true);
			dp.initialsT.setFocusable(true);
			dp.idNumberT.setFocusable(true);
			dp.phoneOneT.setFocusable(true);
			dp.phoneTwoT.setFocusable(true);
			dp.emailT.setFocusable(true);
			dp.titleJCB.setFocusable(true);
			dp.custStatusJCB.setFocusable(true);
			dp.jdc.setFocusable(true);
			ap1.streetT.setFocusable(true);
			ap1.suburbT.setFocusable(true);
			ap1.cityT.setFocusable(true);
			ap1.posCodeT.setFocusable(true);
			dp.newPasswordB.setEnabled(true);
		}
		else if(tf ==  false)
		{
			dp.clientIDT.setFocusable(false);
			dp.surnameT.setFocusable(false);
			dp.nameT.setFocusable(false);
			dp.initialsT.setFocusable(false);
			dp.idNumberT.setFocusable(false);
			dp.phoneOneT.setFocusable(false);
			dp.phoneTwoT.setFocusable(false);
			dp.emailT.setFocusable(false);
			dp.titleJCB.setFocusable(false);
			dp.custStatusJCB.setFocusable(false);
			dp.jdc.setFocusable(false);
			ap1.streetT.setFocusable(false);
			ap1.suburbT.setFocusable(false);
			ap1.cityT.setFocusable(false);
			ap1.posCodeT.setFocusable(false);
			dp.newPasswordB.setEnabled(false);

		}
		
	}
	public void emptyAll()
	{
		dp.clientIDT.setText("");
		dp.surnameT.setText("");
		dp.nameT.setText("");
		dp.initialsT.setText("");
		dp.idNumberT.setText("");
		dp.phoneOneT.setText("");
		dp.phoneTwoT.setText("");
		dp.emailT.setText("");
		dp.titleJCB.setSelectedItem("");
		dp.custStatusJCB.setSelectedItem("");
		dp.jdc.setDate(null);
		ap1.streetT.setText("");
		ap1.suburbT.setText("");
		ap1.cityT.setText("");
		ap1.posCodeT.setText("");
		searchT.setText("");
	}
	
	public void actionPerformed(ActionEvent e)
	{
		Object source = e.getSource();
		if(source == editB)
		{
			if(client == null)
			{
				return;
			}
			setFieldsSelectable(true);
			delB.setEnabled(false);
			dp.validate();
			ap1.validate();
			buttonP.removeAll();
			buttonP.add(saveB);
			buttonP.add(delB);
			this.validate();
			this.repaint();
		}
		
		if(source == saveB)
		{
			if(runCheck()==false)
			{
				JOptionPane.showMessageDialog(
					this,"Please ensure all fields marked "+
					"with * have been filled");
				return;
			}
			Date date = dp.jdc.getDate();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			String bdate = sdf.format(date);
			Date now = new Date();
			String jdate = sdf.format(now);
			String surname = dp.surnameT.getText();
			double credit = client.getCreditAmount();
			String imagePath = client.getImagePath();
			String [] attr = new String [18];
			attr[0] = dp.clientIDT.getText();
			attr[1] = client.getPassword();
			attr[2] = dp.idNumberT.getText();
			attr[3] = dp.surnameT.getText();
			attr[4] = dp.nameT.getText();
			attr[5] = dp.initialsT.getText();
			attr[6] = (String)dp.titleJCB.getSelectedItem();
			attr[7] = ap1.streetT.getText();
			attr[8] = ap1.suburbT.getText();
			attr[9] = ap1.cityT.getText();
			attr[10] = ap1.posCodeT.getText();
			attr[11] = dp.phoneOneT.getText();
			attr[12] = dp.phoneTwoT.getText();
			attr[13] = dp.emailT.getText();
			attr[14] = bdate;
			attr[15] = jdate;
			attr[16] = imagePath;
			attr[17] = (String)dp.custStatusJCB.getSelectedItem();

			client = new Client(attr, credit);
			cm.update(client);
			buttonP.removeAll();
			buttonP.add(editB);
			buttonP.add(delB);
			JOptionPane.showMessageDialog(this, "Saved");
			setFieldsSelectable(false);
			String[] input = cm.getInfoPanel(client.getClientId());
			eastP.removeAll();
			cp = new SideInfoPanel("Client",conn,input);
			eastP.add(cp);
			this.repaint();
			this.validate();
			this.validate();
			this.repaint();
		
		}
		if(source == dp.newPasswordB)
		{
			int choice;
			choice = JOptionPane.showConfirmDialog(this,
								"This will reset the employee password"+
								"\nAre you sure you wish to proceed?",
								"Password Reset",
								JOptionPane.OK_CANCEL_OPTION);
			if(choice == 0)
			{
				String pass = cm.generateClientPassword();
				String [] tempA = client.getClientAttrString();
				tempA[1] = pass;
				client = new Client(tempA);
				cm.update(client);
				String plainPass = cm.getClientPlainTextPassword();
				JOptionPane.showMessageDialog(this, 
								"The password was reset to: "+plainPass,
								"Reset successful",
								JOptionPane.INFORMATION_MESSAGE);
				buttonP.removeAll();
				buttonP.add(editB);
				buttonP.add(delB);
				this.validate();
				this.repaint();
				emptyAll();
				setFieldsSelectable(false);
			}
			else
			{
				return;
			}


		}
		
		if((source == searchB)||(source == searchT))
		{
			String criteria = (String)jcb.getSelectedItem();
			System.out.println(criteria);
			eastP.removeAll();
			String searchText = searchT.getText();
			System.out.println(searchText);
			if((criteria.equals("Last Name"))||(criteria.equals("Phone No")))
			{
				custTable = new JTable();
				custTable = cm.searchClient(searchText, criteria);
				if(custTable.getRowCount() > 1)
				{
					System.out.println("in row table");
					custDialog = new JDialog();
					custDialog.setLayout(new BorderLayout());
					JPanel dialogButtonPanel = new JPanel(new GridLayout(1,2));
					dialogButtonPanel.add(dialogAddB);
					dialogButtonPanel.add(dialogCancelB);
					JScrollPane custJSP = new JScrollPane(custTable);
					custJSP.setHorizontalScrollBarPolicy(
						ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
					custJSP.setVerticalScrollBarPolicy(
						ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
					custDialog.add(custJSP, BorderLayout.CENTER);
					custDialog.add(dialogButtonPanel, BorderLayout.SOUTH);
					custDialog.setSize(380,250);
					custDialog.setLocationRelativeTo(null);
					custDialog.setVisible(true);
					return;
				}
			}
			client = cm.findClient(searchText, criteria);
			if(client == null)
			{
				JOptionPane.showMessageDialog(this, 
						"Couldn't find customer", "Error", 
						JOptionPane.ERROR_MESSAGE);
				return;
			}

			dp.clientIDT.setText(client.getClientId());
			dp.surnameT.setText(client.getLastName());
			dp.nameT.setText(client.getFirstName());
			dp.initialsT.setText(client.getInitials());
			dp.idNumberT.setText(client.getIdNum());
			dp.phoneOneT.setText(client.getPhone1());
			dp.phoneTwoT.setText(client.getPhone2());
			dp.emailT.setText(client.getEmail());
			dp.titleJCB.setSelectedItem(client.getTitle());
			dp.custStatusJCB.setSelectedItem(client.getCustStatus());
			dp.jdc.setFocusable(false);
			try
			{
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				Date date = sdf.parse(client.getBirthDate());
				dp.jdc.setDate(date);
			}
			catch(ParseException pe)
			{
				pe.printStackTrace();
			}

			ap1.streetT.setText(client.getAddress1());
			ap1.suburbT.setText(client.getAddress2());
			ap1.cityT.setText(client.getAddress3());
			ap1.posCodeT.setText(client.getAddress4());
			String[] input = cm.getInfoPanel(client.getClientId());
			cp = new SideInfoPanel("Client",conn,input);
			eastP.add(cp);
			searchT.setText("");
			this.repaint();
			this.validate();
		}
		if(source == dialogAddB)
		{
			try
			{
				int rowIndex = custTable.getSelectedRow();
				searchT.setText((String)custTable.getValueAt(rowIndex,0));
				jcb.setSelectedItem("Client ID");
				searchB.doClick();
				custDialog.dispose();
			}
			catch(Exception ex)
			{
				
			}
		}
		if(source == dialogCancelB)
		{
			custDialog.dispose();
		}
	}
	public void focusGained(FocusEvent fe)
	{
		Object source = fe.getSource();
		if(source == dp.surnameT)
		{
			dp.surnameT.selectAll();
		}
		if(source == dp.nameT)
		{
			dp.nameT.selectAll();
		}
		if(source == dp.initialsT)
		{
			dp.initialsT.selectAll();
		}
		if(source == dp.phoneOneT)
		{
			dp.phoneOneT.selectAll();
		}
		if(source == dp.phoneTwoT)
		{
			dp.phoneTwoT.selectAll();
		}
		if(source == dp.emailT)
		{
			dp.emailT.selectAll();
		}
		if(source == ap1.streetT)
		{
			ap1.streetT.selectAll();
		}
		if(source == ap1.suburbT)
		{
			ap1.suburbT.selectAll();
		}
		if(source == ap1.cityT)
		{
			ap1.cityT.selectAll();
		}
		if(source == ap1.posCodeT)
		{
			ap1.posCodeT.selectAll();
		}
	}
	public void focusLost(FocusEvent e)
	{
		Object source = e.getSource();
		if(source == dp.surnameT)
		{
			dp.surnameT.select(0,0);
		}
		if(source == dp.nameT)
		{
			dp.nameT.select(0,0);
		}
		if(source == dp.initialsT)
		{
			dp.initialsT.select(0,0);
		}
		if(source == dp.phoneOneT)
		{
			dp.phoneOneT.select(0,0);
		}
		if(source == dp.phoneTwoT)
		{
			dp.phoneTwoT.select(0,0);
		}
		if(source == dp.emailT)
		{
			dp.emailT.select(0,0);
		}
		if(source == ap1.streetT)
		{
			ap1.streetT.select(0,0);
		}
		if(source == ap1.suburbT)
		{
			ap1.suburbT.select(0,0);
		}
		if(source == ap1.cityT)
		{
			ap1.cityT.select(0,0);
		}
		if(source == ap1.posCodeT)
		{
			ap1.posCodeT.select(0,0);
		}
	}
	public boolean runCheck()
	{
		if((dp.surnameT.getText().equals(""))||
		(dp.nameT.getText().equals(""))||
		(dp.idNumberT.getText().equals(""))||
		(dp.phoneOneT.getText().equals("")))
		{
			return false;
		}
		if(dp.jdc.getDate() == null)
		{
			return false;
		}
		if(dp.titleJCB.getSelectedItem().equals(""))
		{
			return false;
		}
		if(dp.custStatusJCB.getSelectedItem().equals(""))
		{
			return false;
		}
		else
		{
			return true;
		}
	}
}